<?php
/**
 * Application user
 */
class Application_Model_User extends Zend_Db_Table_Abstract {

    protected $_name = 'users';
    protected $_dependentTables = array('UserProfiles');
    public $id;

    /**
     * Add a User Record
     * @param string $email
     * @param string $role
     * @param string $enabled
     * @return int $user_id
     */
    public function addRecord( $email, $password, $status='active' )
    {
	$keys = $this->generateKeys($email);
	$insert_data = array(
	    "email"	    => $email,
	    "password"	    => $this->createPassword($email, $password),
	    "status"	    => $status,
	    "public_key"    => $keys['public_key'],
	    "private_key"   => $keys['private_key'],
	    "salt"	    => $this->createSalt($email),
	    "created"	    => new Zend_Db_Expr('CURRENT_TIMESTAMP')
	);
	$user_id = $this->insert($insert_data);
	$this->id = $user_id;
	return $user_id;
    }

    /**
     * Update a User Record
     * @param int $user_id
     * @param string $email
     * @param string $role
     * @param int $enabled
     * @return int $result
     */
    public function updateRecord( $user_id, $email, $role, $enabled='1' )
    {
	// Set the row id
	$where = $this->getAdapter()->quoteInto( 'user.id = ?', $user_id );
	// Update the table data
	$update_data = array(
	    "email"	    => $email,
	    "permission"=> $role,
	    "enabled"   => $enabled
	);
	/*
	foreach($data as $field => $value){
		$cols[]= "$field =?";
		if ($field == 'password'){
			$params[]=$this->createPassword($data['email'], $data['password']);
		} else {
			$params[]= $value;
		}
	}
	 * 
	 */
	$result = $this->update($update_data, $where);
	return $result;
    }

    /**
     * Return a User Record
     * @param int $user_id
     * @return Zend_Db_Rowset $result
     */
    public function getById( $user_id )
    {
	$select = $this->select()->where('user.id = ?',$user_id);
	$result = $this->fetchRow($select);
	return $result;
    }

    /**
     * Lookup a User Record
     * @param int $email
     * @return Zend_Db_Rowset $result
     */
    public function getByEmail( $email )
    {
	$select = $this->select()->where('user.email = ?',$email);
	$result = $this->fetchRow($select);
	return $result;
    }

    /**
     * Delete a User Record
     * @param int $user_id
     * @return int $result
     */
    public function deleteRecord( $user_id )
    {
	$where = $this->getAdapter()->quoteInto('user.id = ?', $user_id);
	$result = $this->delete($where);
	return $result;
    }


    /**
     * login
     * @param data array
     * @return array
     */
    public function authenticate($email, $password)
    {
	$password_hash = $this->createPassword($email, $password);
	$select = $this->select()
		->where('users.email = ?', $email)
		->where('users.password = ?',$password_hash);
	$result = $this->fetchRow($select);
	return $result;
    }


    /**
     * getId
     * @param email string
     * @return integer
     */
    public function getId($email)
    {
	$select = $this->select()
		->from('user',array('id'))
		->where('user.email = ?',$email);
	$result = $this->fetchRow($select);
	if ( !$record = $result->current())
	    return false;
	return $record->id;
    }


    private function generateKeys($email)
    {
	return array(
	    "public_key"    => $this->generatePublicKey($email),
	    "private_key"   => $this->generatePrivateKey($email)
	);
    }

    private function generatePublicKey($email)
    {
	return md5($email);
    }
    
    private function generatePrivateKey($email)
    {
	return sha1($email);
    }

    /**
     * createPassword --  generates a hashed string for storing and retrieving passwords
     * @param email string
     * @param password string
     * @return string
     */
    private function createPassword($email, $password)
    {
	return md5($password.$this->createSalt($email));
    }

    /**
     * createSalt
     * @param email string
     * @return string
     */
    private function createSalt($email)
    {
	$substr = substr($email, 4);
	$ciphr = ord($substr) * 58.682;
	return sha1($ciphr);

    }
        
}
